package Controler;

import java.util.ArrayList;

import Client.BusinessImplements;
/**
 /**
 * @author Lucie
 *class Add_new_item_controler
 *Class variables:msg,flag
 *Class methods:get_data,get_all_subjects,get_all_authors,check_input,String get_msg,set_msg
 */
 
public class Add_new_item_controler
{
	private String msg; 
	private boolean flag;
	
	/**
	 *  Constructor initializes the class variables
	 */
	public Add_new_item_controler ()
	{
		flag=true;
		msg=new String();
		msg=null;
	}
	/**
	 * 
	 * @return String white all of the problems that occurred in the class
	 */
	public String get_msg()
	{
		return msg;
	}
	/**
	 * 
	 * @param new_msg
	 * This method chain messages into a string ,
	 *  the purpose ultimately is to display on the screen all the failures
	 */
	public void set_msg(String new_msg)
	{
		if (msg==null)
			msg=new_msg;
		else
		msg+=new_msg;
		
	}
/**
 * 
 * @param item_name
 * @param item_adition
 * @param location
 * @param wanted
 * @param item_subject
 * @param author_name
 * @param description
  * this  method call the class ;serverproxy, to Contact with the server,
  * first we check the Parameters we get (check_input) if something is wrong we return false 
	 *than we continue and add the item to the db (addItem)if something go wrong we return false 
	 * @return  boolean:If the calls in the method failed it returns false otherwise returns true
 */
	public boolean get_data(String item_name, String item_adition,String location,String wanted,String[] item_subject,String[] author_name,String description,String pdf)
	{
		 ArrayList<String> str_item= new  ArrayList<String>();
		 ArrayList<String> str_subjects=new  ArrayList<String>();
		 ArrayList<String> str_authors=new  ArrayList<String>();
		 
		 check_input( item_name,item_adition, location,wanted,item_subject,author_name, description,pdf);
		 if (flag==false)
			 return false;
		 
		 str_item=BusinessImplements.GetInstance().getServerProxy().addItem( item_name,item_adition, location,wanted, description,pdf);
		 
		 if (str_item.get(0).equals("false"))
		 {
			 set_msg("\nadd item faild "); 
			 return false;
		 }
		 
		 
		 str_authors=BusinessImplements.GetInstance().getServerProxy().addItemAuthor(str_item.get(0), author_name);
		 str_subjects=BusinessImplements.GetInstance().getServerProxy().addItemSubject(str_item.get(0), item_subject);
			return flag;
	}
	
/**
 	 *this  method call the class ;serverproxy, to Contact with the server,
	 * we call the method getAuthors() that return from the db all the authors that exist in the db
	 *  
	 * @return ArrayList<String> with all the authors names and id
 */
	public   ArrayList<String> get_all_authors()
	{
		 ArrayList<String> str_authors=new  ArrayList<String>(); 

		str_authors=BusinessImplements.GetInstance().getServerProxy().getAuthors();
		
		
		return str_authors;
		
		
	}
	/**
 	 *this  method call the class ;serverproxy, to Contact with the server,
	 * we call the method getSubjects() that return from the db all the subjects that exist in the db
	 * @return ArrayList<String> with all the subjects names and id
	 */
	public ArrayList<String> get_all_subjects()
	{
		 ArrayList<String> str_subjects=new  ArrayList<String>();
		
		 str_subjects=BusinessImplements.GetInstance().getServerProxy().getSubjects();
	
		return str_subjects;
		
	}

	/**
	 * @param item_name
	 * @param item_adition
	 * @param location
	 * @param wanted
	 * @param item_subject
	 * @param author_name
	 * @param description
	 * This method checks the parameters we have received are not empty
	 * if they are poses flag= false;
	 */
	public void check_input(String item_name, String item_adition,String location,String wanted,String[] item_subject,String[] author_name,String description,String pdf)
	{
		//
		if(item_name.equals(""))
		{
			set_msg("\nmising book name ");
			flag=false;
		}
		//
		if(item_adition.equals(""))
		{
			set_msg("\nmissing book adition ");
			flag=false;
		}
		//
		if(location.equals(""))
		{
			set_msg("\nmissing location ");
			flag=false;
		}
		//
		if(wanted.equals(""))
		{
			set_msg("\nmissing wanted ");
			flag=false;
		}
		if(item_subject.length==0)
		{
			set_msg("\nmissing subjects ");
			flag=false;
		}
		if(author_name.length==0)
		{
			set_msg("\nmissing author name ");
			flag=false;
		}
		//
		if(pdf==null)
		{
			set_msg("\nmissing pdf");
			flag=false;
		}
		
		if(description.equals(""))
		{
			set_msg("\nmissing description");
			flag=false;
		}
		
	}

	

}
